#include "uti_tradeseries_mq.h"
#include "uti_interface.h"
#include "uti_consolecolor.h"

TradeSeries_Mq::TradeSeries_Mq(TradeSerInfo* pserinfo, HANDLE* phan):m_phan_callback(phan)
{

	if (!GetTradeSeries(pserinfo, &m_pser))
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		cerr<<"[TradeSeries_Mq] GetTradeSeries Failed"<<endl;
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  

		return;

	}


	m_pstart = (m_pser->m_ptradecur - m_pser->m_tradeindex);

	m_index = 0;

	*m_phan_callback = CreateEvent(NULL, false, false, NULL);

	m_size = m_pser->m_tradeserinfo.sersize;
};

TradeSeries_Mq::TradeSeries_Mq(unsigned int sersize, void (*pcallback_fun)(), HANDLE* phan):m_phan_callback(phan)
{
	if (!GetTradeSeries(sersize, pcallback_fun, &m_pser))
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		cerr<<"[TradeSeries_Mq] GetTradeSeries Failed"<<endl;
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  

		return;

	}


	m_pstart = (m_pser->m_ptradecur - m_pser->m_tradeindex);

	m_index = 0;

	*m_phan_callback = CreateEvent(NULL, false, false, NULL);

	m_size = m_pser->m_tradeserinfo.sersize;
}



TradeSeries_Mq::~TradeSeries_Mq()
{
	if (NULL != m_pser)
		delete m_pser;


	CloseHandle(*m_phan_callback);
};

int TradeSeries_Mq::CheckReadindex()
{
	int ret = 0;

	m_index++;

	if (m_index == m_size)
	{
		SetEvent(*m_phan_callback);

		m_index = 0;

		ret = 1;
	}

	return ret;

}